package v3.chapter2.setNewStringTwoLock;

public class Run {

	public static void main(String[] args) throws InterruptedException {
		MyService service=new MyService();
		ThreadA a=new ThreadA(service);
		a.setName("A");
		ThreadB b=new ThreadB(service);
		b.setName("B");
		a.start();
		//Thread.sleep(50);
		b.start();
	}

}

class ThreadA extends Thread{
	private MyService service;
	public ThreadA(MyService service) {
		super();
		this.service=service;
	}
	
	@Override
	public void run() {
		service.testMethod();
	}
}

class ThreadB extends Thread{
	private MyService service;
	public ThreadB(MyService service) {
		super();
		this.service=service;
	}
	
	@Override
	public void run() {
		service.testMethod();
	}
}

class MyService{
	private String lock="123";
	public void testMethod() {
		try {
			synchronized(lock) {
				System.out.println(Thread.currentThread().getName()+" begin "+System.currentTimeMillis());
				lock="456";
				Thread.sleep(2000);
				System.out.println(Thread.currentThread().getName()+" end "+System.currentTimeMillis());
			}
		}catch(InterruptedException e) {
			e.printStackTrace();
		}
	}
}